import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user' 
import RegisterView from '../views/user/RegisterView.vue'
import DeviceBookingList from '../views/device/deviceBookingList.vue'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect:'/device'// 访问根路径时重定向到设备页面
    },
    {
      path: "/device",
      name: "device-index",
      component: () => import("../views/device/indexView.vue"),
      meta: { 
        requiresAuth: true,
        title:"设备管理"
       }, // 需要认证
      children:[
        {
          path: "",// 设备列表
          name: "device-list",
          component: () => import("../views/device/deviceListView.vue"),
          meta: { 
            requiresAuth: true ,
            title:"设备列表"}
        },
        {
          path: "bookings", // 用户预约记录
          name: "user-bookings",
          component: () => import("../views/device/UserBookingsView.vue"),
          meta: { 
            requiresAuth: true,
            title: "我的预约"
          }
        },
        {
          path: "/device/bookings",
          name: "DeviceBookings",
          component: () => import('../views/device/deviceBookingList.vue'),
          meta: {
            requiresAuth: true
          }
        },
        {
          path: ":id",// 设备详情
          name: "device-detail",
          component: () => import("../views/device/devicedetailView.vue"),
          meta: { 
            requiresAuth: true,
            title:"设备详情"
           }
        },
        {
          path: "add",// 添加设备
          name: "device-add",
          component: () => import("../views/device/deviceAddView.vue"),
          meta: { 
            requiresAuth: true,
            title:"添加设备"
          }
        },
        {
          path: "edit/:id",// 编辑设备
          name: "device-edit",
          component: () => import("../views/device/deviceEditView.vue"),
          meta: { 
            requiresAuth: true,
             title:"编辑"
          }
        }
      ]
    },
    {
      path:"/user",
      name:"user-index",
      component: () => import("../views/user/indexView.vue"),
       meta: {
        requiresAuth: true,
        title: "我的"
      },  
      children:[
        {
          path: "",
          name: "user-profile",
          alias: "profile",
          component: () => import("../views/user/ProfileView.vue"),
          meta: {
            requiresAuth: true,
            title: "个人信息"
          }
        },
        {
          path: "change-profile/:id",
          name: "user-profile-edit",
          component: () => import("../views/user/ProfileEditView.vue"),
          meta: {
            requiresAuth: true,
            title: "修改个人信息"
          }
        },
        {
          path: "change-password/:id",
          name: "user-password-edit",
          component: () => import("../views/user/PasswordEditView.vue"),
          meta: {
            requiresAuth: true,
            title: "修改密码"
          }
        },
        {
          path: "reviews",
          name: "user-reviews",
          component: () => import("../views/devicefeekdback/userReviewListView.vue"),
          meta: {
            requiresAuth: true,
            title: "我的评论"
          }
        }
      ]
      
    },
    {
      path: "/login",
      name: "user-login",
      component: () => import("../views/user/LoginView.vue"),
      meta:{
        requiresAuth: false,
         title:"登录"
      }
    },
    {
      path: "/admin",
      name: "admin-index",
      component: ()=> import("../views/admin/IndexView.vue"),
      meta: {
        requiresAuth: true,
        requiresAdmin: true,
        title: "系统管理"
      },
      children:[
        {
          path: "",
          name: "user-list",
          alias: "user",
          component: ()=> import("../views/admin/UserListView.vue"),
          meta: {
            requiresAuth: true,
            requiresAdmin: true,
            title: "用户管理"
          },
        },
        {
          path: "bookings",
          name: "admin-bookings",
          component: DeviceBookingList,
          meta: {
            requiresAuth: true,
            requiresAdmin: true,
            title: "预约管理"
          }
        },
        {
          path: "comments",
          name: "admin-comments",
          component: () => import("../views/devicefeekdback/reviewListView.vue"),
          meta: {
            requiresAuth: true,
            requiresAdmin: true,
            title: "评论管理"
          }
        }
      ]
    },
    {
      path: '/about',// 关于页面
      name: 'about',
      component: () => import('../views/AboutView.vue'),
      meta:{
        requiresAuth: false,
         title:"关于"
      }
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView
    },
    { 
      path: '/:pathMatch(.*)*',// 404页面
       name: 'not-found', 
       component: ()=>import('../views/NotFoundView.vue'),
       meta:{
        requiresAuth: false,
         title:"404"
      }
    },
   
  ],
})

//前置路由守卫
router.beforeEach((to)=>{
  const store = useUserStore();
  const isLogin = store.isLogin;
  const isAdmin = store.isAdmin;
  console.log(`to:${to.fullPath},isLogin:${isLogin}`);
  //登录状态服务器校验
  if(to.meta.requiresAuth && isLogin){
    console.log("后端校验权限");
    store.isAuth();
  }
  //防止登录页循环访问
  if (isLogin && to.name === "user-login") {
    return { path: "/" };
  }
 
  //未登录且需要访问权限，跳转到登录路由
  if(to.meta.requiresAuth && !isLogin){
    return {
      path: "/login"
    }
  }
  //需要管理员权限，跳转用户个人信息路由
  if(to.meta.requiresAdmin && !isAdmin){
    return{
      path: "/user"
    }
  }
})
export default router
